<?php
namespace Admin\Controller;

class ProfileController extends CommonController
{
    public function index()
    {
        $list = M("administrator")->select();

        $this->assign("list",$list);
        $this->display("index");
    }
    public function delete()
    {
        $id = $_GET["id"];

        $rs = M("administrator")->where("id = {$id}")->delete();

        if($rs >0)
        {
            $this->success("删除成功!",U("profile/index"));
        }
        else
        {
            $this->success("删除失败!",U("profile/index"));
        }
    }
    //修改管理员信息
    Public function rename()
    {
        $id = $_GET["id"];
        $user = M("administrator")->where("id = {$id}")->find();

        $this->assign("user",$user);
        $this->display("rename");
    }
    public function rename1()
    {
        $id = $_GET["id"];
        $password = M("administrator")->where("id = {$id}")->getField("password");
        $pd1 = $_POST["password"];
        if($password == md5($pd1))
        {
            $arr["username"] = $_POST["username"];
            $arr["password"] = MD5($_POST["password2"]);
            $rs = M("administrator")->where("id = {$id}")->save($arr);
            if($rs > 0)
            {
                $this->success("修改成功!",U("profile/index"));
            }
            else
            {
                $this->success("修改失败!",U("profile/rename",array("id"=>$id)));
            }
        }
        else
        {
            $this->success("原密码不对!",U("profile/rename",array("id"=>$id)));
        }
    }
    //添加管理员
    public function add()
    {
        $this->display("add");
    }
    public function add1()
    {
        $arr["username"] = $_POST["username"];
        $arr["password"] = $_POST["password"];
        $password2 = $_POST["password2"];
        if($arr["password"] == $password2)
        {
            $rs = M("administrator")->add($arr);
            if($rs>0)
            {
                $this->success("添加成功!",U("profile/index"));
            }
            else
            {
                $this->success("添加失败!",U("profile/add"));
            }
        }
        else
        {
            $this->success("两次密码不一致!",U("profile/add"));
        }
    }

}